package com.tablefood.service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.tablefood.dao.FavoriteDao;
import com.tablefood.domain.ShopFavorite;
import com.tablefood.service.FavoriteService;
import com.tablefood.utils.PageCartBean;
import com.tablefood.utils.PageFavBean;
@Transactional
public class FavoriteServiceImpl implements FavoriteService {

	@Resource
	private FavoriteDao  favDao;
	
	@Override
	public boolean addFavorite(ShopFavorite favorite) {
		 
		return favDao.addFavorite(favorite);
	}

	@Override
	public ShopFavorite searchByProductid(String productid) {
		 
		return favDao.searchByProductid(productid);
	}

	@Override
	public PageFavBean queryForPage(String userid, int pageSize, int page) {
		
		final String hql = "from ShopFavorite u where u.shopUser.userid = ?"; // 查询语句
		
		int allRow = favDao.getAllRowCount(hql,userid); // 总记录数
		
		int totalPage = PageCartBean.countTatalPage(pageSize, allRow); // 总页数
		final int offset = PageCartBean.countOffset(pageSize, page); // 当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageCartBean.countCurrentPage(page); // 当前页
		List list = favDao.queryForPage(hql,userid, offset, length); //

		// 把分页信息保存到Bean当中
		PageFavBean pageFavBean = new PageFavBean();
		pageFavBean.setPageSize(pageSize);
		pageFavBean.setCurrentPage(currentPage);
		pageFavBean.setAllRow(allRow);
		pageFavBean.setTotalPage(totalPage);
		pageFavBean.setList(list);
		pageFavBean.init();
		return pageFavBean;
	}

	@Override
	public boolean delFavorite(ShopFavorite favorite) {
		 
		return favDao.delFavorite(favorite);
	}

	 

}
